#include "serverorder.h"
#include "mutexsocketlist.h"
#include "../clientserver/toolsclientserver.h"
/*#include "filemanagement.h"*/

void beginingDealing_Server(Server *server, int socket)
{
  if(isMaxConnected_MutexSocketList(&(server->mutexSocketList))
          == MUTEXSOCKETLIST_NO)
    {
      char *msg = "HELO\n";
      if(send_ToolsCS(socket, msg) != TOOLSCSRETURN_SUCCESS)
	{
	  fprintf(stderr, "[beginingDealing_Server]Erreur send_Tools HELO\n");
	  shutdown(socket, SHUT_RDWR);
	  close(socket);
	}
      else
	{
	  int ret;
	  ret = insert_MutexSocketList(&(server->mutexSocketList), &socket);
	  addConnection_MutexSocketList(&(server->mutexSocketList));
	  if(ret == MUTEXSOCKETLIST_ERR)
	    {
          fprintf(stderr, "[routineThreadNewConnexion_Server]Erreur"
                  " inset_MutexList HELO\n");
	    }
	}
    }
  else
    {
      fprintf(stderr, "(beginingDealing_Server)Information client rejetè car"
              " trop de connexion.\n");
      if(send_ToolsCS(socket, "FAIL\n") != TOOLSCSRETURN_SUCCESS)
	{
	  fprintf(stderr, "[beginingDealing_Server]Erreur send_ToolsCS FAIL\n");
	}
      shutdown(socket, SHUT_RDWR);
      close(socket);
    }
}

ServerOrderReturn dealing_Server(int socket, Server *server)
{
  ServerReturn retOrder;
  
  retOrder = handleOrder_Server(socket);
  switch(retOrder)
    {
    case SERVERORDER_LIST :
      if(sendList_Server(socket, &(server->lFolder))
              != SERVERORDERRETURN_SUCCESS)
	{
	  fprintf(stderr, "[dealing_Server]Erreur sendList_Server\n");
	  return SERVER_ERR;
	}
      break;
    case SERVERORDER_GETF :
      break;
    case SERVERORDER_FAIL :
      if(send_ToolsCS(socket, "FAIL\n") == TOOLSCSRETURN_ERR)
	fprintf(stderr, "[dealing_Server]Erreur send_ToolsCS FAIL\n");
      break;
    case SERVERORDER_QUIT :
      quit_ServerOrder(socket);
      return SERVERORDERRETURN_CLIENTQUIT;
    case SERVERORDER_ERR :
    default :
      shutdown(socket, SHUT_RDWR);
      close(socket);
      return SERVERORDERRETURN_ERR;
    }
  return SERVERORDERRETURN_SUCCESS;
}

void quit_ServerOrder(int socket)
{
  const char *msg = "OLEH\n";
  if(send_ToolsCS(socket, msg) != TOOLSCSRETURN_SUCCESS)
    fprintf(stderr, "[dealing_Server]Erreur send_ToolsCS OLEH\n");
  shutdown(socket, SHUT_RDWR);
  close(socket);
}

ServerOrder handleOrder_Server(int socket)
{
  char *reception = NULL;
  ToolsCSReturn retRecv;
  char *response;
  ServerOrder cmd;

  retRecv = receive_ToolsCS(&reception, socket);
  if(retRecv == TOOLSCSRETURN_ERR)
    return SERVERORDER_ERR;
  response = (char *) malloc ((7 + strlen(reception)) * sizeof(char));
  if(response == NULL)
    {
      fprintf(stderr, "[handleOrder_Server]Erreur malloc\n");
      return SERVERORDER_ERR;
    }
  sprintf(response, "RCPT %s\n", reception);
  if(send_ToolsCS(socket, response) == TOOLSCSRETURN_ERR)
    {
      fprintf(stderr, "[handleOrder_Server]Erreur send_ToolsCS %s\n", response);
      free(response);
      free(reception);
      return SERVERORDER_ERR;
    }
  free(response);
  cmd = orderString_Server(reception);
  free(reception);
  return cmd;
}

ServerOrder orderString_Server(const char *reception)
{
  if(reception == NULL)
    return SERVERORDER_FAIL;
  if(strncmp(reception, "LIST", 4) == 0)
    {
      if(strlen(reception) == 4)
	return SERVERORDER_LIST;
    }
  else if(strncmp(reception, "GETF", 4) == 0)
    return SERVERORDER_GETF;
  else if(strncmp(reception, "QUIT", 4) == 0)
    return SERVERORDER_QUIT;
  return SERVERORDER_FAIL;
}

ServerOrderReturn sendList_Server(int socket, List *lFolder)
{
    (void)socket;
    (void)lFolder;
  return SERVERORDERRETURN_SUCCESS;
  /*
  List list;
  char *message;
getFile_FileManagement(&list, lFolder);


  if(isEmpty_List(&list) != LIST_EMPTY)
    {
      first_List(&list);
      do
	{
	  char *file = getDataCurrent_List(&list);
	  message = (char *) malloc ((strlen(file) + 8) * sizeof(char));
	  
	  sprintf(message, "FICH %s\n", file);
	  if(send_ToolsCS(socket, message) == TOOLSCSRETURN_ERR)
	    {
	      fprintf(stderr, "[sendList_Server]Erreur send_ToolsCS %s\n", message);
	      free(message);
	      testament_List(&list);
	      return SERVERORDERRETURN_ERR;
	    }
	  free(message);
	}while(next_List(&list) == LIST_SUCCESS);
      testament_List(&list);
    }
  if(send_ToolsCS(socket, "ENDL\n") == TOOLSCSRETURN_ERR)
    {
      fprintf(stderr, "[sendList_Server]Erreur send_ToolsCS ENDL\n");
      return SERVERORDERRETURN_ERR;
    }
  return SERVERORDERRETURN_SUCCESS;
  */
}
